﻿<%@ Page Title="" Language="C#" MasterPageFile="BGMasterPage.master" AutoEventWireup="true"
    CodeFile="CategoryPermissions.aspx.cs" Inherits="CategoryPermissions" %>

<asp:Content ID="Content1" ContentPlaceHolderID="cphHeader" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cphContent" runat="Server">
    <table id="jqGridId">
    </table>
    <div id="pager">
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Bottom" runat="Server">
    <script type="text/javascript">
        $(function () { 
        $("#jqGridId").jqGrid({   
        url: "Handler/CategoryHandler.ashx?mode=getDataList",
        editurl:"Handler/CategoryHandler.ashx?mode=edit",
        datatype:"json", //为local时初始化不加载，支持json，xml等   
        mtype: "POST",   shrinkToFit:true,autoload:false,
        colNames:['编号', '名称', '名片数量','禁止访问用户','禁止访问的用户分组'], //表头   
        colModel:[ //这里会根据index去解析jsonReader中root对象的属性，填充cell   
            {name:'ID', index:'User.ID', width:55, align:"center",sortable:false,key:true},  
            {name:'Name', index:'Name', width:120, sortable:false,editable: false},   
            {name:'Count', index:'Count', width:120, sortable:false,editable: false},   
            {name:'UsersID', index:'UsersID', width:120,align:"right",formatter:checkboxFmatter, sortable:false,editable: true,edittype:"custom",editoptions:{custom_element: MultiCheckElem, custom_value:MultiCheckValUser,list:"<%=AllUsers %>"}},   
            {name:'GroupsID', index:'GroupsID', width:120,align:"right",formatter:checkboxFmatter, sortable:false,editable: true,edittype:"custom",editoptions:{custom_element: MultiCheckElem, custom_value:MultiCheckValGroup,list:"<%=AllGroups %>"}},   
        ],   
        width: $("#jqGridId").parent().width(), //数字 & 'auto','100%'   
        height: $(window).height()-90,   
        rowNum: 100, //每页记录数   
        rowList:[5, 10,50,100], //每页记录数可选列表   
        pager: '#pager', //分页标签divID   
        viewrecords: true, //显示记录数信息，如果这里设置为false,下面的不会显示 recordtext: "第{0}到{1}条, 共{2}条记录", //默认显示为{0}-{1} 共{2}条 scroll: false, //滚动翻页，设置为true时翻页栏将不显示  
        /**这里是排序的默认设置，这些值会根据列表header点击排序时覆盖*/ sortable: false,   
        sortname: "id",   
        sortorder: "desc",   
  
        caption:"用户列表", //显示查询结果表格标题   
        rownumbers: true, //设置列表显示序号,需要注意在colModel中不能使用rn作为index   
        rownumWidth: 20, //设置显示序号的宽度，默认为25   
        multiselect: true, //多选框   
        multiboxonly: true, //在点击表格row时只支持单选，只有当点击checkbox时才多选，需要multiselect=true是有效   
        prmNames : { //如当前查询实体为ware，这些可以在查询对象的superObject中设定   
            page: "wareDetail.page",   
            rows: "wareDetail.rows",   
            sort: "wareDetail.sidx",   
            order: "wareDetail.sord",   
            search: "wareDetail.search"   
        },   
        jsonReader:{ //server返回Json解析设定   
            root: "list", //对于json中数据列表   
            page: "page",   
            total: "totalPage",   
            records: "totalCount",  
            repeatitems: false,   
        },
        editSubmit : function(response, postdata) 
        { 
        alert("submit");
        return [true,"test"] 
        }
    });   
    var editOptions={ top: 100, left: ($("#jqGridId").width()-500)/2, width: 500,closeOnEscape: true,closeAfterEdit:true,reloadAfterSubmit:false, afterSubmit: function(response, postdata){
        var data = $.parseJSON(response.responseText);
        return[data.Result,data.Message,data.ID];
//        return[false,data.Message,data.ID];
    }};
    $("#jqGridId").jqGrid('navGrid','#pager',{edit:true,add:false,del:false,search:true},editOptions,editOptions,editOptions);//这里设定分页bar显示的信息
    
    });

function MultiCheckElem( value, options )
{
   var ctl = '';
   if(options.list==""){return '<br />&nbsp;';}
   var ckboxAry = options.list.split(',');
   for ( var i in ckboxAry )
   {
      var item = ckboxAry[i];
      var keyValue=item.split(':');
      ctl += '<input type="checkbox" ';
      if ( value.indexOf(keyValue[0]) != -1 )
         ctl += 'checked="checked" '+ ' title="'+keyValue[0]+'" ';
      ctl += 'value="' + keyValue[1] +'"> ' + keyValue[0] + '</input>&nbsp;';
   }

   ctl = ctl.replace( '<br />&nbsp;', '' );
   return ctl;
}

function MultiCheckValGroup(elem, action, val)
{
   var items = '';
   if (action == 'get') // the form has been submitted
   {
      $("[name='GroupsID']").each(function(){if(this.checked)items+=$(this).val()+",";});
      items = items.replace(/,$/, '');
   }
   else // the form is launched
   {
      for (var i in elem)
      {
         if (elem[i].tagName == 'INPUT')
         {
            if (val.indexOf(elem[i].title) == -1)
               elem[i].checked = false;
            else
               elem[i].checked = true;
         }
      } // endfor
   }
   return items;
}
function MultiCheckValUser(elem, action, val)
{
   var items = '';
   if (action == 'get') // the form has been submitted
   {
      $("[name='UsersID']").each(function(){if(this.checked)items+=$(this).val()+",";});
      items = items.replace(/,$/, '');
   }
   else // the form is launched
   {
      for (var i in elem)
      {
         if (elem[i].tagName == 'INPUT')
         {
            if (val.indexOf(elem[i].title) == -1)
               elem[i].checked = false;
            else
               elem[i].checked = true;
         }
      } // endfor
   }
   return items;
}
function checkboxFmatter (cellvalue, options, rowObject)
{
    if(cellvalue==null){return "";}
    var new_format_value="";
   var keyValue=options.colModel.editoptions.list.split(',');
   for(var i in keyValue){
        var values=keyValue[i].split(":");
        if(cellvalue.toString().indexOf(values[1])>-1){
            new_format_value+=values[0]+",";
        }
   }
  return new_format_value.substring(0,new_format_value.length-1);
}
    </script>
</asp:Content>
